package com.hmall.user.interceptor;

import com.hmall.user.context.BaseContext;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String authorization = request.getHeader("authorization");

        if (StringUtils.isEmpty(authorization)){
            return false;
        }

        Long userId = Long.valueOf(authorization);

        BaseContext.setUserId(userId);

        return true;
    }
}
